package 小测试;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
 * Created by Administrator on 2017/8/6.
 */
public class CollectionTest {
    public static void main(String[] args) {
        //创建并初始化集合
        ArrayList arrayList = createArrayList();
        //遍历集合
        printArrayList(arrayList);

    }

    private static void printArrayList(ArrayList arrayList) {
        //1、while方式
        Iterator iterator = arrayList.iterator();
        while(iterator.hasNext()){

            Object next = iterator.next();
            System.out.println(next);
        }
        //2、for方式
        for(Iterator iterator1=arrayList.iterator();iterator1.hasNext();){
            System.out.println(iterator1.next());
        }
        //3、转换为数组
        Object[] objects = arrayList.toArray();
        System.out.println(Arrays.toString(objects));
    }

    private static ArrayList createArrayList() {
        ArrayList<Object> arrayList = new ArrayList<>();
        arrayList.add(1);//集合中是不能存储基本数据，但是却可以存储对应的包装类
        //查看编译后的class文件发现，这里进行了自动装箱：Integer.valueOf(1)
        arrayList.add("lucy");
        return arrayList;
    }
}
